package cn.zuo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
/**
 * @Author 白白旧维
 * @Date 2021/7/7 11:13
 */
public class MySqlSession {//连接数据库的

    private static String resource = "mybatis_config.xml";
    private static SqlSessionFactory sqlSessionFactory = null;

    static {//通过配置文件初始化SqlSession工厂
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//通过输入流获取sqlsession工厂
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession openSqlSession(){//打开一个会话
        return sqlSessionFactory.openSession();
    }

    public static void closeSqlSession(SqlSession sqlSession){//关闭一个会话
        sqlSession.close();
    }

}
